﻿using System.Data;

namespace Loki.Data
{
    /// <summary>
    /// Interface for database adapters.
    /// </summary>
    public interface IDatabaseAdapter
    {
        /// <summary>
        /// Initialize the database (if available).
        /// </summary>
        void Initialize();

        bool Initialized { get; }

        /// <summary>
        /// Gets the datasource description.
        /// </summary>
        string DataSourceDescription { get; }

        /// <summary>
        /// Gets the current user.
        /// </summary>
        string CurrentUser { get; }

        /// <summary>
        /// Gets or sets the connection string.
        /// </summary>
        /// <value>
        /// The connection string.
        /// </value>
        string ConnectionString
        {
            get;
            set;
        }

        /// <summary>
        /// Creates the command.
        /// </summary>
        /// <param name="P_Name">Name of the command.</param>
        /// <returns>The command.</returns>
        IDatabaseCommand CreateCommand(string P_Name);

        /// <summary>
        /// Creates the command.
        /// </summary>
        /// <param name="P_Name">Name of the command.</param>
        /// <param name="P_Type">Type of the command.</param>
        /// <returns>The command.</returns>
        IDatabaseCommand CreateCommand(string P_Name, CommandType P_Type);

        /// <summary>
        /// Bulks insert in the database.
        /// </summary>
        /// <param name="P_Table">The table.</param>
        /// <returns>The number of updated lines.</returns>
        int BulkInsert(DataTable P_Table);

        #region Query execution
        /// <summary>
        /// Executes command.
        /// </summary>
        /// <param name="P_Command">The command.</param>
        /// <param name="P_Transaction">The transaction.</param>
        void ExecuteNonQuery(IDatabaseCommand P_Command, IDbTransaction P_Transaction);

        /// <summary>
        /// Executes the query.
        /// </summary>
        /// <param name="P_Command">The command.</param>
        /// <param name="P_Transaction">The transaction.</param>
        /// <returns>The recordset.</returns>
        IDataReader ExecuteQuery(IDatabaseCommand P_Command, IDbTransaction P_Transaction);

        /// <summary>
        /// Executes the query and return an unique value.
        /// </summary>
        /// <param name="P_Command">The command.</param>
        /// <param name="P_Transaction">The transaction.</param>
        /// <returns>The query result.</returns>
        object ExecuteScalar(IDatabaseCommand P_Command, IDbTransaction P_Transaction);

        #endregion

        #region Transaction

        /// <summary>
        /// Opens a new transaction on the database.
        /// </summary>
        /// <returns>The transaction.</returns>
        IDbTransaction OpenTransaction();

        /// <summary>
        /// Commits the transaction.
        /// </summary>
        /// <param name="P_Tran">The transaction.</param>
        /// <returns>True if the transaction commits successfully.</returns>
        bool CommitTransaction(IDbTransaction P_Tran);

        /// <summary>
        /// Rollbacks the transaction.
        /// </summary>
        /// <param name="P_Tran">The transaction.</param>
        /// <returns>True if the transaction rollbacks successfully.</returns>
        bool RollbackTransaction(IDbTransaction P_Tran);

        #endregion
    }
}
